/*
#include <jni.h>
#include <cv.h>
#include <stdlib.h>
#include <highgui.h>


JNIEXPORT jint JNICALL Java_com_opencvtest_opencvtest_getKeypointNum(JNIEnv* env,jobject thiz)
{
    IplImage* object = cvLoadImage( "/mnt/sdcard/sho1.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    int i=0;
    for(i=0;i<2;i++){
	    const char* input_name="/mnt/sdcard/sho1.jpg";
	    const char* filename = input_name;
	    IplImage* img = cvLoadImage( filename, 1 );
	    cvSaveImage("/mnt/sdcard/dexamp316.jpg",img,0);
	    cvReleaseImage( &img );
	    return i;
    }

    return 0;
}
*/

#include <jni.h>
#include <stdio.h>

#include "cv.h"
#include "highgui.h"
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>


static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
 
void detect_and_draw( IplImage* image );
 
const char* cascade_name =
    "haarcascade_frontalface_alt.xml";

//JNIEXPORT jstring JNICALL 
JNIEXPORT jint JNICALL Java_com_opencvtest_opencvtest_getKeypointNum(JNIEnv* env,jobject thiz)
{

    CvCapture* capture = 0;
    const char* input_name="/mnt/sdcard/sho1.jpg";
/* 
    cascade_name = "haarcascade_frontalface_alt.xml";
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
 
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
    }
    storage = cvCreateMemStorage(0);
 
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else
        capture = cvCaptureFromAVI( input_name ); 
 */
//    cvNamedWindow( "result", 1 );

    const char* filename = input_name;
    IplImage* img = cvLoadImage( filename, 1 );

    FILE* f = fopen( filename, "rt" );
    if( f )
     {
    		char buf[1000+1];
    		while( fgets( buf, 1000, f ) )
            {
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';

/************************************************************************/
//                    detect_and_draw( image );
int detect_and_draw( IplImage* img )
{
    static CvScalar colors[] = 
    {
        {{0,0,255}},
        {{0,128,255}},
        {{0,255,255}},
        {{0,255,0}},
        {{255,128,0}},
        {{255,255,0}},
        {{255,0,0}},
        {{255,0,255}}
    };
 
    double scale = 1.3;
    IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
    IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
                         cvRound (img->height/scale)),8, 1 );
    int i;
 
    cvCvtColor( img, gray, CV_BGR2GRAY );
    cvResize( gray, small_img, CV_INTER_LINEAR );
    cvEqualizeHist( small_img, small_img );
    cvClearMemStorage( storage );
 
    if( cascade )
    {
        double t = (double)cvGetTickCount();
        CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
                                            1.1, 2,0,cvSize(30, 30) );
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (faces ? faces->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
        }
    }

   // cvShowImage( "result", img );
    cvReleaseImage( &gray );
    cvReleaseImage( &small_img );
}


/************************************************************************/

                    cvSaveImage("/mnt/sdcard/dexamp317.jpg",img,0);
          //          cvWaitKey(0);
                    cvReleaseImage( &img );

             }
           fclose(f);
     }
    cvDestroyWindow("result");
	return 0;

}
